package com.jwsoft.manager.core.dao.service.impl;

import com.jwsoft.manager.core.dao.service.TableService;
import com.jwsoft.manager.core.util.SpecialStringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 用于获取 Spring 的应用上下文
 * 根据表名来动态调用具体的service进行处理
 * @Author zhiyuan
 * @Date 2023/9/14 9:50
 */
@Component
public class TableServiceFactory implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }
    public static TableService getTableService(String tableName) {
        String serviceBeanName = SpecialStringUtils.tableToCamelCase(tableName) + "ServiceImpl";
        TableService bean = applicationContext.getBean(serviceBeanName, TableService.class);
        return bean;
    }
}
